<?php

namespace api\modules\v2\controllers;



use api\modules\v2\resources\search\UserProfileSearch;


use api\modules\v2\resources\Upload;
use common\models\UserProfile;
use Intervention\Image\ImageManagerStatic;
use trntv\filekit\actions\DeleteAction;
use trntv\filekit\actions\UploadAction;
use yii\data\ActiveDataFilter;


use yii\filters\auth\CompositeAuth;
use yii\filters\auth\HttpBasicAuth;
use yii\filters\auth\HttpBearerAuth;
use yii\filters\auth\HttpHeaderAuth;
use yii\filters\auth\QueryParamAuth;
use yii\helpers\Json;
use yii\rest\Controller;
use yii\web\UploadedFile;
use Yii;

class ProfileController extends Controller
{

    public $serializer = [
        'class' => 'yii\rest\Serializer',
        'collectionEnvelope' => 'items',
    ];
    public function behaviors()
    {
        $behaviors = parent::behaviors();

        $behaviors['authenticator'] = [
            'class' => CompositeAuth::class,
            'authMethods' => [
                HttpBasicAuth::class,
                HttpBearerAuth::class,
                HttpHeaderAuth::class,
                QueryParamAuth::class
            ]
        ];

        return $behaviors;
    }



//    public function actions()
//    {
//        return [
//            'upload' => [
//                'class' => UploadAction::class,
////                'deleteRoute' => 'avatar-delete',
////                'on afterSave' => function ($event) {
////                    /* @var $file \League\Flysystem\File */
////                    $file = $event->file;
////                    $img = ImageManagerStatic::make($file->read())->fit(215, 215);
////                    $file->put($img->encode());
////                }
//            ],
//            'avatar-delete' => [
//                'class' => DeleteAction::class
//            ]
//        ];
//    }



    public function actionIndex()
    {

        print_r(Yii::$app->getUser());die;
        $filter = new ActiveDataFilter([
            'searchModel' => 'api\modules\v2\resources\search\UserProfileSearch',
        ]);

        $filterCondition = null;

        if ($filter->load(\Yii::$app->request->get())) {
            $filterCondition = $filter->build();
            if ($filterCondition === false) {
                return $filter;
            }
        }
        $searchParams = [];
        if (!empty($filterCondition)) {
            foreach ($filterCondition as $key => $value) {
                if(is_array($value)){
                    $searchParams['UserProfileSearch'][key($value)] = $value[key($value)];
                }else{
                    $searchParams['UserProfileSearch'][$key] = $value;
                }
            }
        }

        $searchModel  = new UserProfileSearch();
        $dataProvider = $searchModel->search($searchParams,$filterCondition);
        return $dataProvider;
    }

    public function actionView($user_id){
        $searchParams['UserProfileSearch']['user_id'] = $user_id;
        $searchModel  = new UserProfileSearch();
        $dataProvider = $searchModel->search($searchParams);
        return $dataProvider;
    }
    public function actionUpload(){

//        $model = new Upload();
//        $model->imageFile = UploadedFile::getInstance($model, 'file');
//        if ($model->upload()) {
//            // 文件上传成功
//            return;
//        }

//
        $uploadedFile = UploadedFile::getInstanceByName('file');

        $uploadDiir = Yii::getAlias('@storage').'/web/source';
        $filename= md5(time()).'.jpg';
        $uploadedFile->saveAs($uploadDiir.'/'.$filename);

        $user_id = 5;
        $profile = UserProfile::findOne(['user_id'=>$user_id]);
//        $profile->avatar_base_url= Yii::getAlias('@storageUrl');
//        $profile->avatar_path=$filename;

        $profile->picture =[
            'path'=>'/'.$filename,
            'base_url'=>Yii::getAlias('@storageUrl').'/source'
        ];

       if(!$profile->save()){

           print_r($profile->errors);
       }else{
           echo 33;die;
       }



//
//        print_r($_FILES);
//        print_r($_POST);die;
    }

    public function actionUpdate(){

        $body = Yii::$app->request->getRawBody();
        $postArray =Json::decode($body);
        //$reg(Json::decode($body));die;
       // print_r($postArray);
        $user_id = 5;
        $profile = UserProfile::findOne(['user_id'=>$user_id]);
        $profile->setAttributes($postArray);
      //  print_r($profile->getAttributes());die;
//        $profile->gender = 1;
        $profile->save();
    }
}
